﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Configuration;
using System.Runtime.Serialization;
using Microsoft.Http;
using System.Runtime.Serialization.Json;

namespace ScrumFactory.Services.Clients {

    [Export(typeof(ITeamService))]
    public class TeamServiceClient : ITeamService {

        private Uri baseUrl;

        [ImportingConstructor()]
        public TeamServiceClient([Import("TeamServiceUrl")] string url)
            : this(new Uri(url)) {
        }

        public TeamServiceClient(Uri url) {
            this.baseUrl = url;
        }


        public MemberProfile GetMember(string memberUId) {
            var client = new HttpClient(new Uri(this.baseUrl, "Members/" + memberUId));
            HttpResponseMessage response = client.Send(HttpMethod.GET);
            RestServiceUtil.HandleHTTPErrorCode(response);
            return response.Content.ReadAsJsonDataContract<MemberProfile>();
        }

        public ICollection<MemberProfile> GetAllMembers() {
            var client = new HttpClient(new Uri(this.baseUrl, "Members"));
            HttpResponseMessage response = client.Send(HttpMethod.GET);
            RestServiceUtil.HandleHTTPErrorCode(response);
            return response.Content.ReadAsJsonDataContract<ICollection<MemberProfile>>();
        }

        public ICollection<MemberProfile> GetProjectMembers(string projectUId) {
            var client = new HttpClient(new Uri(this.baseUrl, "Members/?projectUId=" + projectUId.Trim()));
            HttpResponseMessage response = client.Send(HttpMethod.GET);
            RestServiceUtil.HandleHTTPErrorCode(response);
            return response.Content.ReadAsJsonDataContract<ICollection<MemberProfile>>();
        }


        public void AddProjectMembership(string memberUId, ProjectMembership membership) {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.POST, new Uri(this.baseUrl, "Members/" + memberUId.Trim() + "/Memberships"), HttpContentExtensions.CreateJsonDataContract(membership));
            RestServiceUtil.HandleHTTPErrorCode(response);
        }

        public void UpdateProjectMembership(string memberUId, ProjectMembership membership) {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.PUT, new Uri(this.baseUrl, "Members/" + memberUId.Trim() + "/Memberships"), HttpContentExtensions.CreateJsonDataContract(membership));
            RestServiceUtil.HandleHTTPErrorCode(response);
        }

        public void RemoveProjectMembership(string memberUId, ProjectMembership membership) {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.DELETE, new Uri(this.baseUrl, "Members/" + memberUId.Trim() + "/Memberships"), HttpContentExtensions.CreateJsonDataContract(membership));
            RestServiceUtil.HandleHTTPErrorCode(response);
        }

    }
}
